<template>
  <div class="info-container">
    <el-card style="height: 100%">
      <search-tool @onSearch="handleSearch"></search-tool>
      <div class="btns">
        <div class="add-btn" @click="handlePub">发布</div>
      </div>

      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        height="70%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="序号" type="index" width="55" />
        <el-table-column prop="screenCode" label="发布屏编号" width="150">
        </el-table-column>
        <el-table-column prop="screenName" label="发布屏名称" width="180">
        </el-table-column>
        <el-table-column prop="deviceType" label="设备类型" width="100">
        </el-table-column>
        <el-table-column prop="specType" label="规格型号" width="150">
        </el-table-column>
        <el-table-column prop="region" label="所属区域" width="120">
        </el-table-column>
        <el-table-column prop="lampPostName" label="所属灯杆" width="180">
        </el-table-column>
        <el-table-column prop="locationInfo" label="位置信息" width="150">
        </el-table-column>
        <el-table-column prop="mediaName" label="发布内容" width="150">
        </el-table-column>
        <el-table-column label="设备状态" width="150">
          <template #default="{ row }">
            <span
              :style="
                row.deviceState === 0
                  ? { color: '#3EC155' }
                  : { color: '#999999' }
              "
              >{{ row.deviceState === 0 ? '在线' : '离线' }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="{ row }">
            <div class="action-row">
              <span class="action-btn" @click="handleEdit(row)">查看</span>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <view-dialog
      :title="dialogTitle"
      :rowData="curRowData"
      v-model:visible="visibleDialog"
      @handlePub="viewJumpPub"
    ></view-dialog>
    <pub-dialog
      title="发布屏设置"
      :datas="selections"
      v-model:visible="visiblePubDialog"
      @handlePub="handlePublish"
    ></pub-dialog>
    <ing-dialog
      title="发布中"
      :datas="pubDatas"
      :name="curName"
      :contentType="curType"
      :isClose="visibleIngDialog"
      v-model:visible="visibleIngDialog"
      @closeOther="closeOtherDialog"
    ></ing-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import SearchTool from './components/SearchTool/index.vue'
import ViewDialog from './components/ViewDialog/index.vue'
import PubDialog from './components/PubDialog/index.vue'
import IngDialog from './components/IngDialog/index.vue'
import { pubscreenList } from '@/api/back'
import { message } from '@/utils/message'

//数据相关
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(7)
const params = ref({})
const loading = ref(false)
const visibleDialog = ref(false)
const visiblePubDialog = ref(false)
const visibleIngDialog = ref(false)
const curRowData = ref({})
const selections = ref([])
const dialogTitle = ref('')
const curName = ref('')
const curType = ref('')
const pubDatas = ref([])

//获取数据的方法
const getListData = async (params) => {
  loading.value = true
  const result = await pubscreenList(params, page.value, size.value)
  loading.value = false
  tableData.value = result.rows
  total.value = result.total
}

const reloadPage = () => {
  getListData(params.value)
  if (visibleDialog.value) {
    visibleDialog.value = false
  }
}

reloadPage()
//查询条件相关
const handleSearch = (paramsData) => {
  params.value = paramsData
  getListData(paramsData)
}

// 发布中弹窗
const handlePublish = (name, type, datas) => {
  curName.value = name
  curType.value = type
  pubDatas.value = datas
  visibleIngDialog.value = true
}

// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}

const handlePub = () => {
  if (selections.value.length < 1) {
    message({ type: 'warning', message: '请选择发布屏' })
    return
  }
  visiblePubDialog.value = true
}

// 查看弹窗的设置按钮跳转打开发布弹窗
const viewJumpPub = () => {
  selections.value = [curRowData.value]
  visibleDialog.value = false
  visiblePubDialog.value = true
}
const handleEdit = (row) => {
  dialogTitle.value = '查看发布屏'
  curRowData.value = row
  visibleDialog.value = true
}

const handleSelectionChange = (selection) => {
  selections.value = selection
}
const closeOtherDialog = () => {
  visibleDialog.value = false
  visiblePubDialog.value = false
}
</script>

<style lang="scss" scoped>
.info-container {
  width: 100%;
  height: 100%;
  .btns {
    width: 100%;
    display: flex;
    height: 40px;
    align-items: center;
    margin-bottom: 20px;
    .add-btn {
      width: 80px;
      height: 36px;
      font-size: 16px;
      font-family: Microsoft YaHei;
      color: #ff9502;
      box-sizing: border-box;
      cursor: pointer;
      border-radius: 5px;
      background: rgba(255, 255, 255, 0.39);
      border: 1px solid #ff9502;
      box-shadow: 0px 0px 4px rgba(206, 206, 206, 0.3);
      text-align: center;
      line-height: 36px;
      &:hover {
        background: #ff950236;
      }
    }
    .del-btn {
      @extend.add-btn;
      background: rgba(255, 255, 255, 0.39);
      border: 1px solid #cecece;
      box-shadow: 0px 0px 4px rgba(206, 206, 206, 0.3);
      color: #999999;
      margin-left: 10px;
      &:hover {
        background: #99999936;
      }
    }
  }

  .action-row {
    display: flex;
    .action-btn {
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 19px;
      color: #ff9502;
      cursor: pointer;
    }
    .has-more {
      width: 2px;
      height: 10px;
      background: #ff9e02;
      margin-right: 10px;
      margin-left: 10px;
      margin-top: 5px;
    }
  }
  .pagination {
    width: 100%;
    height: 32px;
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: end;
  }
}
</style>
